{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入必要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.read_csv(\"./RentListingInquries_FE_train.csv\")\n",
    "test = pd.read_csv(\"./RentListingInquries_FE_test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = train[:2000]\n",
    "test = test[:1000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_train = train['interest_level']\n",
    "X_train = train.drop(['interest_level'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "reg_alpha = [1.5,2]\n",
    "reg_lambda = [0.5,1,2]\n",
    "\n",
    "param_test = dict(reg_alpha = reg_alpha,reg_lambda= reg_lambda)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\train\\Anaconda2\\lib\\site-packages\\sklearn\\model_selection\\_search.py:761: DeprecationWarning: The grid_scores_ attribute was deprecated in version 0.18 in favor of the more elaborate cv_results_ attribute. The grid_scores_ attribute will not be available from 0.20\n",
      "  DeprecationWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "([mean: -0.67963, std: 0.01243, params: {'reg_alpha': 1.5, 'reg_lambda': 0.5},\n",
       "  mean: -0.68070, std: 0.01247, params: {'reg_alpha': 1.5, 'reg_lambda': 1},\n",
       "  mean: -0.68019, std: 0.01254, params: {'reg_alpha': 1.5, 'reg_lambda': 2},\n",
       "  mean: -0.68088, std: 0.01108, params: {'reg_alpha': 2, 'reg_lambda': 0.5},\n",
       "  mean: -0.68109, std: 0.01175, params: {'reg_alpha': 2, 'reg_lambda': 1},\n",
       "  mean: -0.68135, std: 0.01185, params: {'reg_alpha': 2, 'reg_lambda': 2}],\n",
       " {'reg_alpha': 1.5, 'reg_lambda': 0.5},\n",
       " -0.67963323205616322)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xgb = XGBClassifier(\n",
    "    learning_rate = 0.1,\n",
    "    n_estimators = 72,\n",
    "    max_depth = 3,\n",
    "    min_child_weight = 3,\n",
    "    gamma = 0,\n",
    "    subsample = 0.6,\n",
    "    colsample_bytree = 0.8,\n",
    "    colsample_bylevel = 0.7,\n",
    "    objective = 'multi:softprob',\n",
    "    seed=3\n",
    ")\n",
    "gsearch = GridSearchCV(xgb,param_grid=param_test,scoring='neg_log_loss',n_jobs=-1,cv=kfold)\n",
    "gsearch.fit(X_train,y_train)\n",
    "\n",
    "gsearch.grid_scores_, gsearch.best_params_,     gsearch.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
